package cn.tedu.m4;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producter4 {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        //1.创建连接工厂,并设置连接信息
        ConnectionFactory f1 = new ConnectionFactory();
        f1.setHost("192.168.64.140");
        f1.setPort(5672);
        f1.setUsername("admin");
        f1.setPassword("admin");
        Connection con = f1.newConnection(); //连接
        Channel c = con.createChannel(); //通信的通道
        //2.创建交换机
        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        //3.向交换机发送消息，携带关键词（路由键）
        while (true){
            System.out.println("输入消息：");
            String s = new Scanner(System.in).nextLine();
            System.out.println("输入路由键：");
            String k = new Scanner(System.in).nextLine();
            c.basicPublish("direct_logs", k, null, s.getBytes());
        }
    }
}
